﻿using System;
using System.Collections.Generic;
using System.Configuration.Install;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Principal;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GPDWin4GameBarPowerBackend
{
    public static class Program
    {
        public const string serviceName = "GPDWinGameBarPowerBackend";

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        static void Main(string[] args)
        {
            Environment.CurrentDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            bool isService = !Environment.UserInteractive;
            if(isService)
            {

                SvcLog.SvcLog.Initialize(maxLogs: 36);
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[]
                {
                new Service1()
                };
                ServiceBase.Run(ServicesToRun);
            }
            else
            {
                if(args.Length > 0)
                {
                    var type = args[0];

                    if(type == AltF4Sender.Program.type)
                    {
                        AltF4Sender.Program.Main();
                    }
                    if (type == AIO.ForegroundChangedHelper.Program.type)
                    {

                        SvcLog.SvcLog.Initialize(maxLogs: 36);
                        AIO.ForegroundChangedHelper.Program.Main();
                    }
                    if (type == AIO.ManagementUI.Program.type)
                    {
                        AIO.ManagementUI.Program.Main();
                    }
                    if (type == ServiceInstaller.Program.type)
                    {
                        WindowsIdentity identity = WindowsIdentity.GetCurrent();
                        WindowsPrincipal principal = new WindowsPrincipal(identity);
                        if (principal.IsInRole(WindowsBuiltInRole.Administrator))
                        {

                            ServiceInstaller.Program.Main();
                        }
                        else
                        {
                            ProcessStartInfo psi = new ProcessStartInfo();
                            psi.UseShellExecute = true;
                            psi.WorkingDirectory = Environment.CurrentDirectory;
                            psi.FileName = Application.ExecutablePath;
                            psi.Arguments = type;
                            psi.Verb = "runas";
                            try
                            {
                                Process.Start(psi);
                            }
                            catch (Exception) { }
                        }
                    }
                }
                else
                {
                    bool serviceIsRunning = false;
                    try
                    {
                        using (ServiceController control = new ServiceController(GPDWin4GameBarPowerBackend.Program.serviceName))
                        {
                            if (control.Status == ServiceControllerStatus.Running)
                            {
                                serviceIsRunning = true;
                            }
                            
                        }
                    }catch (Exception) { }
                    if(serviceIsRunning)
                    {
                        AIO.ManagementUI.Program.Main();
                    }
                    else
                    {
                        WindowsIdentity identity = WindowsIdentity.GetCurrent();
                        WindowsPrincipal principal = new WindowsPrincipal(identity);
                        if (principal.IsInRole(WindowsBuiltInRole.Administrator))
                        {

                            ServiceInstaller.Program.Main();
                        }
                        else
                        {
                            ProcessStartInfo psi = new ProcessStartInfo();
                            psi.UseShellExecute = true;
                            psi.WorkingDirectory = Environment.CurrentDirectory;
                            psi.FileName = Application.ExecutablePath;
                            psi.Verb = "runas";
                            try
                            {
                                Process.Start(psi);
                            }catch (Exception) { }
                        }
                    }
                }
            }
        }
    }
}
